﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WCFMINERP.Order.Presenter;
using WCFMINERP.MODEL.Order;

namespace WCFMINERP.Order.Forms
{
    public partial class FrmProductTemplate : Form
    {
        public  OrderPresenter _presenter;

        public FrmProductTemplate(OrderPresenter presenter)
        {
            InitializeComponent();
            _presenter = presenter;
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void FrmProductTemplate_Load(object sender, EventArgs e)
        {
            LinkProduct();
            LinkTemplate();
        }
        /// <summary>
        /// 绑定产品列表
        /// </summary>
        private void LinkProduct()
        {
            gcproduct.DataSource = _presenter._producList;
            gcproduct.RefreshDataSource();
        }
        /// <summary>
        /// 绑定模板列表
        /// </summary>
        private void LinkTemplate()
        {
            CreateTemplateControls(_presenter.GetProductTemplateNames());
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTemplate.Text.Trim()))
            {
                txtTemplate.Focus();
                MessageBox.Show("请输入模板名称！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            List<ProductInfo> listProduct = gcproduct.DataSource as List<ProductInfo>;
            List<ProductInfo> SelectedProductList = listProduct.Where(p => p.isChecked == true).ToList();
            List<int> productIdList = new List<int>();
            foreach (var item in SelectedProductList)
	        {
        		 productIdList.Add(item.productid);
	        }
            int ret = _presenter.CreateProductTemplate(txtTemplate.Text, productIdList);
            if (ret == 0)
            {
                FrmOrder frm = this.Owner as FrmOrder;
                frm.linkTemplate.Invoke();//绑定最新模板
                LinkTemplate();
                MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("该模板名称已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
           
        }

        /// <summary>
        /// 更新模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTemplate.Text.Trim()))
            {
                MessageBox.Show("请选择要更新的模板！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<ProductInfo> listProduct = gcproduct.DataSource as List<ProductInfo>;
            List<ProductInfo> SelectedProductList = listProduct.Where(p => p.isChecked == true).ToList();
            List<int> productIdList = new List<int>();
            foreach (var item in SelectedProductList)
            {
                productIdList.Add(item.productid);
            }
            int ret = _presenter.UPdateProductTemplate(txtTemplate.Text, productIdList);
            LinkTemplate();
            MessageBox.Show("更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确定要删除模板：" + txtTemplate.Text.Trim() + "?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                if (string.IsNullOrEmpty(txtTemplate.Text.Trim()))
                {
                    MessageBox.Show("请选择要删除的模板！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                _presenter.DeleteProductTemplate(txtTemplate.Text.Trim());
                LinkTemplate();
                FrmOrder frm = this.Owner as FrmOrder;
                frm.linkTemplate.Invoke();//绑定最新模板
                MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 创建模板按钮
        /// </summary>
        /// <param name="list"></param>
        private void CreateTemplateControls(List<string> list)
        {
            flpTemplate.Controls.Clear();
            foreach (var item in list)
            {
                Button btn = new Button();
                btn.Height = 60;
                btn.Width = 80;
                btn.Text = item;
                btn.Click += new EventHandler(btn_Click);
                flpTemplate.Controls.Add(btn);
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            string templateName =((Button)sender).Text;
            txtTemplate.Text = templateName;
            ProductTemplateInfo templateInfo = _presenter.GetProductTemplateInfo(templateName);
            List<ProductInfo> productInfoList = gcproduct.DataSource as List<ProductInfo>;
            foreach (var item in productInfoList)
            {
                if (templateInfo.ProductIdList.Contains(item.productid))
                    item.isChecked = true;
                else
                    item.isChecked = false;
            }

            gcproduct.RefreshDataSource();

        }
    }
}
